package wangjg.coco.views;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import wangjg.coco.PDUtil;
import wangjg.coco.module.Model2SourceProcessor;
import wangjg.coco.widgets.FileInput;
import wangjg.coco.widgets.FileTreeBox;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * 
 * @author wangjg
 *
 */
public class Model2SourceDialog extends AbstractDialog {
	private static final String PREFERENCE_STORE_PATH = Model2SourceDialog.class.getName();
	
	private static final String M2S_MODEL_PATH = "m2s_model_path";
	private static final String M2S_TEMPL_PATH = "m2s_templ_path";
	
	private Composite composite1;
	private Composite composite_bottom;
	private Group modelGroup;
	private Group templateGroup;
	
	private FileInput modelDirInput;
	private FileTreeBox modelTreeBox;
	
	private FileTreeBox templateTreeBox;
	private FileInput templateDirInput;
	
	private Label outputLabel = null;
	private Text outputText = null;
	private String selectedPath;

	public Model2SourceDialog(Shell parentShell) {
		super(parentShell);
		this.setShellStyle(getShellStyle() | SWT.RESIZE);
	}
	
	public void setSelectedPath(String selectedPath) {
		this.selectedPath = selectedPath;
	}

	protected void okPressed(){
		Model2SourceProcessor mp = new Model2SourceProcessor();
		
		mp.setTemplateDir(templateDirInput.getText().getText());
		mp.setTemplSelection(templateTreeBox.getSelectedFiles());
		
		mp.setModelDir(this.modelDirInput.getText().getText());
		mp.setModelFileList(this.modelTreeBox.getSelectedFiles());
		
		mp.setTargetDir(outputText.getText());
		mp.setTargetCharset(PDUtil.getCharset());
		
		mp.setSeparate(true);
		
		mp.process();
		
		this.saveSetting();
		
		super.okPressed();
	}
	
	protected Control createDialogArea(Composite parent) {
		Composite composite = (Composite) super.createDialogArea(parent);
		composite.setLayout(getGridLayout(1));
		
		Composite composite_main = new Composite(composite, SWT.NONE);
		composite_main.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		FillLayout layout = new FillLayout(SWT.VERTICAL);
		composite_main.setLayout(layout);
		
		composite1 = this.getFillComposite(composite_main, SWT.HORIZONTAL);
		{
			modelGroup = this.getFillGroup(composite1, SWT.VERTICAL);
			modelGroup.setText("model");
			{
				modelDirInput = new FileInput(modelGroup, SWT.NONE);
				modelDirInput.setMulti(true);
				this.setGridData(modelDirInput, 1);
				modelTreeBox = new FileTreeBox(modelGroup, SWT.NONE, true, null);
				this.setGridData(modelTreeBox, 1);
			}
			templateGroup = this.getFillGroup(composite1, SWT.VERTICAL);
			templateGroup.setText("templ");
			{
				templateDirInput = new FileInput(templateGroup, SWT.NONE);
				templateDirInput.setMulti(true);
				this.setGridData(templateDirInput, 1);
				templateTreeBox = new FileTreeBox(templateGroup, SWT.NONE, true, null);
				this.setGridData(templateTreeBox, 1);
			}
		}
		composite_bottom = new Composite(composite, SWT.NONE);
		this.setGridData(composite_bottom, 1);
		composite_bottom.setLayout(this.getGridLayout(3));
//		{
			this.outputLabel = new Label(composite_bottom, SWT.BORDER);
			this.outputLabel.setText("输出目录：");
//			this.setGridData(outputLabel, 1);
			this.outputText = new Text(composite_bottom, SWT.BORDER);
			this.outputText.setText(this.selectedPath);
			this.setGridData(outputText, 2);
//		}
		this.setListeners();
		
		this.loadSetting();
		
		return composite;
	}
	
	private void setListeners(){
		Listener cgistener = new Listener(){
			public void handleEvent(Event event) {
				setTreeBox(modelTreeBox, event.data.toString(), modelDirInput.getFileNames());
			}
		};
		this.modelDirInput.setChangeListener(cgistener);
		
		Listener cgistener2 = new Listener(){
			public void handleEvent(Event event) {
				setTreeBox(templateTreeBox, event.data.toString(), templateDirInput.getFileNames());
			}
		};
		this.templateDirInput.setChangeListener(cgistener2);
		
		modelGroup.addListener(SWT.Resize, new Listener () {
	    public void handleEvent (Event e) {
	    	Rectangle rect = modelGroup.getClientArea();
	    	Rectangle rect2 = modelDirInput.getClientArea();
	    	modelTreeBox.getTree().setSize(rect.width-10, rect.height-rect2.height-10);
	      }
	    });
		
		templateGroup.addListener(SWT.Resize, new Listener () {
		    public void handleEvent (Event e) {
		    	Rectangle rect = templateGroup.getClientArea();
		    	Rectangle rect2 = templateDirInput.getClientArea();
		    	templateTreeBox.getTree().setSize(rect.width-10, rect.height-rect2.height-10);
		      }
		    });
	}
	
	private void setTreeBox(FileTreeBox modelTreeBox, String dirPath, String[] names){
		modelTreeBox.getTree().removeAll();
		for(String fileName:names){
			File file = new File(dirPath, fileName);
			modelTreeBox.makeSubItem(file);
		}
//		modelTreeBox.load(path);
		modelTreeBox.setCheckedAll(true);
//		modelTreeBox.setExpandedAll(false);
	}
	
	private Composite getFillComposite(Composite parent, int type){
		Composite composite = new Composite(parent, SWT.NONE);
		FillLayout layout = new FillLayout(type);
		layout.marginWidth = 5;
		layout.marginHeight = 5;
		layout.spacing = 5;
		composite.setLayout(layout);
		return composite;
	}
	
	private GridLayout getGridLayout(int numColumns){
		GridLayout layout = new GridLayout();
		layout.numColumns = numColumns;
		layout.makeColumnsEqualWidth = false;
		return layout;
	}
	
	private Group getFillGroup(Composite parent, int type){
		GridLayout layout = new GridLayout();
		layout.numColumns = 1;
		layout.makeColumnsEqualWidth=false;//?
		
		Group group = new Group(parent, SWT.NONE);
		group.setLayout(layout);
		return group;
	}
	
	
	private void saveSetting(){
		PreferenceStore preferenceStore = new PreferenceStore(PREFERENCE_STORE_PATH);
		preferenceStore.setValue(M2S_MODEL_PATH, this.modelDirInput.getValue());
		preferenceStore.setValue(M2S_TEMPL_PATH, this.templateDirInput.getValue());
		try {
			preferenceStore.save();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void loadSetting(){
		PreferenceStore preferenceStore = new PreferenceStore(PREFERENCE_STORE_PATH);
		try {
			preferenceStore.load();
		} catch (IOException e) {
			e.printStackTrace();
			return ;
		}
		this.modelDirInput.setValue(preferenceStore.getString(M2S_MODEL_PATH));
		this.templateDirInput.setValue(preferenceStore.getString(M2S_TEMPL_PATH));
	}
}
